from django.test import TestCase
from datetime import datetime
from .models import User, Shop, Category, Product, Orders, OrderDetail, Payment


class ModelsTestCase(TestCase):
    def setUp(self):
        # 创建测试数据
        self.user = User.objects.create(username='test_user', nickname='Test User', password_hash='password', password_salt='salt')
        self.shop = Shop.objects.create(name='Test Shop', cover_pic='cover.jpg', banner_pic='banner.jpg', address='Test Address', phone='1234567890')
        self.category = Category.objects.create(shop_id=self.shop.id, name='Test Category')
        self.product = Product.objects.create(shop_id=self.shop.id, category_id=self.category.id, cover_pic='product.jpg', name='Test Product', price=9.99)
        self.order = Orders.objects.create(shop_id=self.shop.id, member_id=1, user_id=self.user.id, money=19.99)
        self.order_detail = OrderDetail.objects.create(order_id=self.order.id, product_id=self.product.id, product_name='Test Product', price=9.99, quantity=2)
        self.payment = Payment.objects.create(order_id=self.order.id, member_id=1, money=19.99, type=1, bank=1)

    def test_user_model(self):
        self.assertEqual(self.user.username, 'test_user')
        self.assertEqual(self.user.nickname, 'Test User')
        self.assertEqual(self.user.password_hash, 'password')
        self.assertEqual(self.user.password_salt, 'salt')

    def test_shop_model(self):
        self.assertEqual(self.shop.name, 'Test Shop')
        self.assertEqual(self.shop.cover_pic, 'cover.jpg')
        self.assertEqual(self.shop.banner_pic, 'banner.jpg')
        self.assertEqual(self.shop.address, 'Test Address')
        self.assertEqual(self.shop.phone, '1234567890')

    def test_category_model(self):
        self.assertEqual(self.category.shop_id, self.shop.id)
        self.assertEqual(self.category.name, 'Test Category')

    def test_product_model(self):
        self.assertEqual(self.product.shop_id, self.shop.id)
        self.assertEqual(self.product.category_id, self.category.id)
        self.assertEqual(self.product.cover_pic, 'product.jpg')
        self.assertEqual(self.product.name, 'Test Product')
        self.assertEqual(self.product.price, 9.99)

    def test_orders_model(self):
        self.assertEqual(self.order.shop_id, self.shop.id)
        self.assertEqual(self.order.member_id, 1)
        self.assertEqual(self.order.user_id, self.user.id)
        self.assertEqual(self.order.money, 19.99)

    def test_order_detail_model(self):
        self.assertEqual(self.order_detail.order_id, self.order.id)
        self.assertEqual(self.order_detail.product_id, self.product.id)
        self.assertEqual(self.order_detail.product_name, 'Test Product')
        self.assertEqual(self.order_detail.price, 9.99)
        self.assertEqual(self.order_detail.quantity, 2)

    def test_payment_model(self):
        self.assertEqual(self.payment.order_id, self.order.id)
        self.assertEqual(self.payment.member_id, 1)
        self.assertEqual(self.payment.money, 19.99)
        self.assertEqual(self.payment.type, 1)
        self.assertEqual(self.payment.bank, 1)